package com.mooc.fuxi.ui.detail;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.mooc.fuxi.DaoMaster;
import com.mooc.fuxi.DaoSession;
import com.mooc.fuxi.R;
import com.mooc.fuxi.entity.CommentEntity;
import com.mooc.fuxi.entity.NewsDetailEntity;
import com.mooc.fuxi.entity.NewsEntity;
import com.mooc.fuxi.mvvm.BaseActivity;
import com.mooc.fuxi.ui.comment.CommentActivity;
import com.mooc.fuxi.ui.login.LoginActivity;
import com.mooc.fuxi.utils.SPUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NedwsDetailActivity extends BaseActivity implements
        View.OnClickListener {

    private NewsDetailViewModel viewModel=new NewsDetailViewModel();
    private WebView webView;
    private EditText comment_edt;
    private View dialogView;
    private Button send_comment_btn;
    private AlertDialog  dialog;
    @Override
    public int bindLayout() {
        return R.layout.activity_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initData();
    }

    private void init(){
        findViewById(R.id.detail_more_btn).setOnClickListener(this::onClick);
        findViewById(R.id.detail_send_btn).setOnClickListener(this::onClick);
        webView = f(R.id.detail_web);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        dialogView = LayoutInflater.from(this)
                .inflate(R.layout.layout_edt_comment,null,false);
        comment_edt = dialogView.findViewById(R.id.comment_edt);
        send_comment_btn = dialogView.findViewById(R.id.send_comment_btn);
        send_comment_btn.setOnClickListener(this::onClick);
    }
    NewsEntity.Values values;
    private void initData(){
        values = (NewsEntity.Values) getIntent()
                .getSerializableExtra("news");
        viewModel.requestDetail(values.newscode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewsDetailEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull NewsDetailEntity newsDetailEntity) {
                        Log.e("ZXY","onNext:"+newsDetailEntity.data.url);
                        //由于接口返回网页地址无法打开
                        newsDetailEntity.data.url = "https://www.baidu.com/";
                        webView.loadUrl(newsDetailEntity.data.url);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.detail_send_btn){
            //登录判断
            if (!SPUtils.getInstance().isLogin("login")) {
                startActivity(new Intent(this, LoginActivity.class));
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(dialogView);
            dialog =  builder.create();
            dialog.show();
        }else if (R.id.detail_more_btn == v.getId()){
            Intent intent= new Intent(this,
                    CommentActivity.class);
            intent.putExtra("title",values.title);
            startActivity(intent);
        }else {
            dialog.dismiss();
            String content = comment_edt.getText().toString();
            CommentEntity entity = new CommentEntity();
            entity.title = content;
            entity.flag = false;
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"user.db");
            DaoMaster master = new DaoMaster(helper.getWritableDb());
            DaoSession session = master.newSession();
            session.getCommentEntityDao().insert(entity);
        }
    }
}
